package com.tiendung.otalunch;

import java.util.ArrayList;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class RatingFood extends Activity
{
	private static final String PASSPHRASE = "You can never guess me";
	private String restaurant_name;
	private String food_name;
	
	private TextView label;
	private RadioButton radio_1;
	private RadioButton radio_2;
	private RadioButton radio_3;
	private RadioButton radio_4;
	private RadioButton radio_5;
	private Button submit;
	private ProgressDialog dialog;	    

	private OtaLunchDatabase db;
	
    @Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rating);		
		restaurant_name = getIntent().getStringExtra("Restaurant");
		food_name = getIntent().getStringExtra("Name");
				
		label = (TextView) findViewById(R.id.label);
		radio_1 = (RadioButton) findViewById(R.id.radio_1);
		radio_2 = (RadioButton) findViewById(R.id.radio_2);
		radio_3 = (RadioButton) findViewById(R.id.radio_3);
		radio_4 = (RadioButton) findViewById(R.id.radio_4);
		radio_5 = (RadioButton) findViewById(R.id.radio_5);
		submit = (Button) findViewById(R.id.submit);
		
		label.setText("You are rating for \"" + food_name + "\" from \"" + restaurant_name + "\"\n");
		this.db = new OtaLunchDatabase(this);
        		
		submit.setOnClickListener(new View.OnClickListener() {             
        	public void onClick(View v) 
        	{                 
        		 dialog = ProgressDialog.show(RatingFood.this, "", "Submit the rating. Please wait...", true);
        	     
        		 new Thread(new Runnable() 
 		        {    
 		    		public void run() {    
		        		 if (radio_1.isChecked()) rate(1); 
		        		 else if (radio_2.isChecked()) rate(2); 
		        		 else if (radio_3.isChecked()) rate(3); 
		        		 else if (radio_4.isChecked()) rate(4); 
		        		 else if (radio_5.isChecked()) rate(5);
		        		 else rate(0);
		        		 
		        		 dialog.cancel();
		        		 finish();
		        	}  
		        }).start();  
        	}
        });    
	}
    
    private void rate(int value)
    {
    	ArrayList<String> facebook_info = db.select_facebook();
    	
    	String url;
    	if (facebook_info.size() > 1)
    		url = "http://" + TheTabWidget.serverIP + "/rate.php?id=" + encrypt(facebook_info.get(0)) + "&name=" + encrypt(facebook_info.get(1)) + "&rate=" + value + "&food=" + food_name + "&restaurant=" + restaurant_name;
    	else
    		url = "http://" + TheTabWidget.serverIP + "/rate.php?id=NULL&name=NULL&rate=" + value + "&food=" + food_name + "&restaurant=" + restaurant_name;
    	
    	try {
    		DefaultHttpClient client = new DefaultHttpClient();
    		url = url.replace(" ", "%20");
        	HttpGet request = new HttpGet(url);	
        	client.execute(request);
		}
    	catch (Exception e) {}
    }
    
    private String encrypt(String input) {
		  DESKeySpec keySpec;
		  try {
				keySpec = new DESKeySpec(PASSPHRASE.getBytes("UTF8"));
				SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
				SecretKey key = keyFactory.generateSecret(keySpec);
				  
				// ENCODE plainTextPassword String
				byte[] cleartext = input.getBytes("UTF8");      
				  
				Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe
				cipher.init(Cipher.ENCRYPT_MODE, key);
				String encrypedPwd = new String(Base64.encodeBase64(cipher.doFinal(cleartext)));		  
				  
				return encrypedPwd;
		  } catch (Exception e) {
				e.printStackTrace();
		  }
		  
		  return null;
	   }
}
